package com.jzy.message.activities;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.jzy.message.R;
import com.jzy.message.activities.base.BaseApplication;
import com.jzy.message.activities.adapter.SessionAdapter;
import com.jzy.message.activities.base.BaseFragment;
import com.jzy.message.control.view.CustomListView;
import com.jzy.message.control.view.TitleBarView;
import com.jzy.message.db.ChatMsgDao;
import com.jzy.message.db.SessionDao;
import com.jzy.message.entity.xmpp.Session;
import com.jzy.message.util.common.CommUtility;
import com.jzy.message.util.xmpp.Const;
import com.jzy.message.util.xmpp.PreferencesUtils;
import com.jzy.message.util.xmpp.XmppUtil;

import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.XMPPException;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/7/2.
 */
public class Message_MessageFragment extends BaseFragment implements CustomListView.OnRefreshListener {
    private Context mContext;
    private View mBaseView;
    private CustomListView mCustomListView;
    private SessionAdapter adapter;
    private List<Session> sessionList = new ArrayList<Session>();
    private TitleBarView mTitleBarView;
    private SessionDao sessionDao;
    private ChatMsgDao chatMsgDao;
    private String userid;

    private AddFriendReceiver addFriendReceiver;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.message_fragment_news_father, container, false);
        mContext = getActivity();
        mBaseView = inflater.inflate(R.layout.message_fragment_news_father, null);
        userid= PreferencesUtils.getSharePreStr(mContext, "username");
        sessionDao=new SessionDao(mContext);
        chatMsgDao=new ChatMsgDao(mContext);
        addFriendReceiver=new AddFriendReceiver();
        IntentFilter intentFilter=new IntentFilter(Const.ACTION_ADDFRIEND);
        mContext.registerReceiver(addFriendReceiver, intentFilter);
        findView();
        initData();
        return mBaseView;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }
    private void findView() {
      /*  mTitleBarView=(TitleBarView) mBaseView.findViewById(R.id.title_bar);
        mTitleBarView.setCommonTitle(View.GONE, View.VISIBLE, View.GONE, View.GONE);
        mTitleBarView.setTitleText(R.string.msgs);*/

        mCustomListView = (CustomListView) mBaseView.findViewById(R.id.lv_news);//listview
        mCustomListView.setOnRefreshListener(this);//设置listview下拉刷新监听
        mCustomListView.setCanLoadMore(false);//设置禁止加载更多
        mCustomListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                final Session session=sessionList.get(arg2-1);
                if(session.getType().equals(Const.MSG_TYPE_ADD_FRIEND)){
                    if(!TextUtils.isEmpty(session.getIsdispose())){
                        if(!session.getIsdispose().equals("1")){
                            Builder bd=new AlertDialog.Builder(mContext);
                            bd.setItems(new String[]{"同意"}, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface arg0, int arg1) {
                                    Roster roster= BaseApplication.xmppConnection.getRoster();
                                    XmppUtil.addGroup(roster, "我的好友");//先默认创建一个分组
                                    if(XmppUtil.addUsers(roster, session.getFrom()+"@"+BaseApplication.xmppConnection.getServiceName(), session.getFrom(),"我的好友")){
                                        //告知对方，同意添加其为好友
                                        new Thread(new Runnable() {
                                            @Override
                                            public void run() {
                                                try {
                                                    //注意消息的协议格式 =》接收者卍发送者卍消息类型卍消息内容卍发送时间
                                                    String message= session.getFrom()+Const.SPLIT+userid+Const.SPLIT+Const.MSG_TYPE_ADD_FRIEND_SUCCESS+Const.SPLIT+""+Const.SPLIT+new SimpleDateFormat("MM-dd HH:mm").format(new Date());
                                                    XmppUtil.sendMessage(BaseApplication.xmppConnection, message, session.getFrom());
                                                } catch (XMPPException e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        }).start();
                                        sessionDao.updateSessionToDisPose(session.getId());//将本条数据在数据库中改为已处理
//										ToastUtil.showShortToast(mContext, "你们已经是好友了，快去聊天吧！");
                                        sessionList.remove(session);
                                        session.setIsdispose("1");
                                        sessionList.add(0,session);
                                        adapter.notifyDataSetChanged();
                                        //发送广播更新好友列表
                                        Intent intent=new Intent(Const.ACTION_FRIENDS_ONLINE_STATUS_CHANGE);
                                        mContext.sendBroadcast(intent);
                                    }else{
                                        CommUtility.showmsg(mContext,"添加好友失败");
                                    }
                                }
                            });
                            bd.create().show();
                        }else{
                            CommUtility.showmsg(mContext,"已同意");
                        }
                    }
                }else{
                    Intent intent=new Intent(mContext, ChatActivity.class);
                    intent.putExtra("from", session.getFrom());
                    startActivity(intent);
                }
            }
        });
        mCustomListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
                final Session session=sessionList.get(arg2-1);
                Builder bd=new AlertDialog.Builder(mContext);
                bd.setItems(new String[] {"删除会话"}, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        sessionDao.deleteSession(session);
                        chatMsgDao.deleteAllMsg(session.getFrom(), session.getTo());
                        mContext.sendBroadcast(new Intent(Const.ACTION_NEW_MSG));
                        initData();
                    }
                }).create().show();
                return true;
            }
        });
    }

    private void initData() {
        //注意，当数据量较多时，此处要开线程了，否则阻塞主线程
        sessionList=sessionDao.queryAllSessions(userid);
        adapter = new SessionAdapter(mContext, sessionList);
        mCustomListView.setAdapter(adapter);
    }

    @Override
    public void onStart() {
        super.onStart();
        initData();
    }

    @Override
    public void onRefresh() {
        initData();
        mCustomListView.onRefreshComplete();
    }

    class AddFriendReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context arg0, Intent intent) {
            initData();
        }

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mContext.unregisterReceiver(addFriendReceiver);
    }
}
